/**
 * 
 */
package com.srs.action;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Timer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.srs.model.User;
import com.srs.util.MailUtil;

/**
 * @author chienvh
 *
 */
public class BaseAction extends ActionSupport implements Preparable{

	private String title;
	public static final int RECORD_PER_PAGE = 15;
	public HttpSession session;
	public HttpServletRequest request;
	public HttpServletResponse response;
	public User loginUser;
	private static final String[] ACTION_NOT_NEED_VALIDATE = {"/user/login.do", "/user/loginComplete.do", "/user/logout.do"};
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	@Override
	public String doDefault() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("Hello");
		return SUCCESS;
	}
	@Override
	public void prepare() throws Exception{
		// TODO Auto-generated method stub
		System.out.println("Prepare");
		request  = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse(); 
		session  = request.getSession();
		loginUser = (User) session.getAttribute("loginUser");		
		/**
		 * Start mail service
		 */
		Map aplication = ServletActionContext.getActionContext(request).getApplication();
		if (aplication.get("isStartMail")==null){
			try {
				System.out.println("Start send mail service");
				System.out.println(ServletActionContext.getServletContext().getRealPath("/"));
//				String filePath = session.
//				getServlet().getServletContext().getRealPath("/")
				MailUtil mailUtil = new MailUtil(new File(ServletActionContext.getServletContext().getRealPath("/") + "mail.xml"));
				mailUtil.getConfig();
				System.out.println("Config time:" + mailUtil.getSendTime());
				
			    Timer timer = new Timer();
			    Calendar date = Calendar.getInstance();
			    date.set(Calendar.DAY_OF_WEEK, new Date().getDay());
			    date.set(Calendar.HOUR, Integer.valueOf(mailUtil.getSendTime().substring(0, 2)));
			    date.set(Calendar.MINUTE, Integer.valueOf(mailUtil.getSendTime().substring(3, 5)));
			    date.set(Calendar.SECOND, Integer.valueOf(mailUtil.getSendTime().substring(6, 8)));
			    date.set(Calendar.MILLISECOND, 0);
			    // Schedule to run every Sunday in midnight
			    timer.schedule(
			    		mailUtil,
			    		date.getTime(),
			    		1000 * 60 * 60 * 24
			    );
			    
				aplication.put("isStartMail", true);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		

		
	}
	
	public boolean isActionNeedCheckAuthenticate(){
		for(int i=0; i<BaseAction.ACTION_NOT_NEED_VALIDATE.length; i++){
			String action = BaseAction.ACTION_NOT_NEED_VALIDATE[i];
			if (request.getRequestURL().toString().indexOf(action)==-1){
				return true;
			}
		}
		return false;
	}
/************************************************************************************/	
	
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}


	/**
	 * @return the session
	 */
	public HttpSession getSession() {
		return session;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(HttpSession session) {
		this.session = session;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
	 */
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;

	}

	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * @return the loginUser
	 */
	public User getLoginUser() {
		return loginUser;
	}

	/**
	 * @param loginUser the loginUser to set
	 */
	public void setLoginUser(User loginUser) {
		this.loginUser = loginUser;
	}

	public static void main(String[] args){
		Date date1 = new Date();
	    Calendar date = Calendar.getInstance();
	    date.set(Calendar.DAY_OF_WEEK, date1.getDay());
	    date.set(Calendar.HOUR, Integer.valueOf("00:00:00".substring(0, 2)));
	    date.set(Calendar.MINUTE, Integer.valueOf("00:00:00".substring(3, 5)));
	    date.set(Calendar.SECOND, Integer.valueOf("00:00:00".substring(6, 8)));		
		System.out.println(Integer.valueOf("00:00:00".substring(0, 2)));
		System.out.println(Integer.valueOf("00:11:00".substring(3, 5)));
		System.out.println(Integer.valueOf("00:00:22".substring(6, 8)));
	    Timer timer = new Timer();
	    MailUtil mailUtil = new MailUtil(new File("mail.xml"));
//	    date.set(Calendar.MILLISECOND, 0);
	    // Schedule to run every Sunday in midnight
	    timer.schedule(
	    		mailUtil,
	    		date.getTime(),
	    		5000
	    );		
	}
}